//任务发送方列表
var taskResponseList = Ext.create('Ext.grid.Panel', {
    store: taskResponseListStore,
//    overflowX: 'hidden', //设置横向滚动条不显示
//    overflowY: 'auto', //设置纵向滚动条超出高度显示
//    height: 490, 
    columns: [
        {
            text: "任务名字", flex: 1, dataIndex: 'name',
            filter: {//用于形成搜索框
                type: 'string',
                emptyText: '输入要搜索的任务名字',
                serializer: function (filter) {
                    return filter.value;
                }
            }
        },
        {text: "状态", dataIndex: 'statusName'},
        {text: "优先级", dataIndex: 'priorityName'}
    ],
    columnLines: true,
    bbar: taskResponseListPagingBar,
    plugins: {
        rowexpander: {
            rowBodyTpl: new Ext.XTemplate(
                '<tpl if="isGroup== 1">',//判断收件人是个人还是组别
                '<b>收件组:</b>',
                '<tpl else>',
                '<b>收件人:</b>',
                '</tpl>',
                '<tpl for="receivers">',//循环输出收件人
                '{receiverName}&nbsp;&nbsp;&nbsp;',
                '</tpl>',
                '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
                '<b>任务类型:</b>',
                '{typeName}<br><br>' +
                '<b>任务创建日期:</b> {createDate:date("Y-m-d H:i:s")}',//2019-08-14 22:19:22
                '<p><b>任务描述:</b> {description}</p>',
                '<b>附件：</b>',
                '<tpl if="isAttachment != 0">',//判断是否有附件
                '<tpl for="attached">',//循环输出附件<a href="javascript:void(0);" onclick="downloadAtta({hashOrUuid},{TA_ATTACHED_NAME},{TA_HASHORUUID})"></a>
                '&nbsp;&nbsp;<a href="javascript:void(0);" onclick="downloadAtta(\'{uuid}\',\'{attachedName}\',\'{fileType}\')">{attachedName}</a>',
                '</tpl>',
                '<tpl else>',
                '<b>无',
                '</tpl>'
            )
        }
    },
    selModel: taskResponseListCheckBox,
    dockedItems: [
        {
            xtype: 'toolbar',
            items: [{
                text: '新建任务',
                tooltip: '新建任务',
                itemId: 'addTaskButton',
                icon: 'static/images/icons/fam/add.gif',
                listeners: {
                    click: function (button, e, eOpts) {
                        suitTaskFormForCreate();
                    }
                }
            }, '-', {
                xtype: 'combo',
                emptyText: '任务状态',
                editable: false,//防止用户直接在下拉框里输入
                store: statusStoreForResponsible,
                width: 90,
                queryMode: 'local',
                displayField: 'statusName',
                valueField: 'statusNum',
                listeners: {
                    change: function (combo, newValue, oldValue, eOpts) {
                        filterResponsibleTaskListByStatus(newValue);
                    }
                }
            }, '-', {
                itemId: 'responserEditButton',
                text: '编辑任务',
                tooltip: '编辑任务',
                icon: 'static/images/icons/fam/plugin.gif',
                disabled: true,
                listeners: {
                    click: function () {
                        var item = taskResponseList.getSelectionModel().getSelected();//得到被选择的行的数组.
                        if (item.items[0].data.statusName != '编辑中') {//先判断该任务是否可编辑
                            Ext.Msg.alert('警告', '此任务无法编辑！');
                        } else {
                            taskForm.items.each(function (item) {
                                item.setDisabled(false);
                            });
                            enableFormBtn();
                            //编辑前先去后台获取该任务全部数据添加到store
                            Ext.getCmp('centerRegion').removeAll(false);//先移除原有的items,不然直接添加会叠加,如果设置为true就是会自动销毁之前的组件，之后就无法在使用这个组件了
                            var taskid = item.items[0].data.id;
                            Ext.Ajax.request({
                                url: zuulUrl + '/' + taskServiceName + '/taskDetailForResponsible/' + taskid,
                                async: false,   //ASYNC 是否异步( TRUE 异步 , FALSE 同步)
                                success: function (response, opts) {
                                    var obj = Ext.decode(response.responseText);
                                    var data = {
                                        'id': obj.task.id,
                                        'name': obj.task.name,
                                        'responsible': obj.task.responsible,
                                        'typeName': obj.task.typeName,
                                        'statusName': obj.task.statusName,
                                        'priorityName': obj.task.priorityName,
                                        'isGroup': obj.task.isGroup,
                                        'description': obj.task.description,
                                        'isAttachment': obj.task.isAttachment,
                                        'createDate': obj.task.createDate,
                                        'receivers': obj.receivers,
                                        'attached': obj.attached,
                                        'isNull': 1
                                    };
                                    item.items[0].set(data);
                                    //将获得的任务的数据填充到表单里面
                                    taskForm.getForm().reset();
                                    taskId = taskid;
                                    taskCreateOrUpdate = 1;//说明这是编辑操作不是新建操作
                                    Ext.getCmp('taskName').setValue(item.items[0].data.name);
                                    var receiverShow = '';
                                    if (item.items[0].data.isGroup == 1) {//根据收件人是组还是个人更换对应的显示
                                        Ext.getCmp('isGroup').down('#no').setValue(false);
                                        Ext.getCmp('isGroup').down('#yes').setValue(true);
                                        taskForm.down('#chooseReceiver').setFieldLabel('收件组');
                                        chooseReceiverGrid.setStore(Ext.data.StoreManager.lookup('receiversGroupStore'));
                                        Ext.data.StoreManager.lookup('receiversGroupStore').reload();
                                        //将任务收件人选择grid对应的收件人设定为选中状态
                                        var receiverArray = new Array();
                                        Ext.each(item.items[0].data.receivers, function (receiver) {
                                            receiverShow = receiverShow + receiver.receiverName + '  ';
                                            receiverArray.push(Ext.data.StoreManager.lookup('receiversGroupStore').findRecord('id', receiver.receiverId));
                                        });
                                        chooseReceiverGrid.setSelection(receiverArray);
                                    } else {
                                        taskForm.down('#chooseReceiver').setFieldLabel('收件人');
                                        chooseReceiverGrid.setStore(Ext.data.StoreManager.lookup('receiversStore'));
                                        Ext.data.StoreManager.lookup('receiversStore').reload();
                                        //将任务收件人选择grid对应的收件人设定为选中状态
                                        var receiverArray = new Array();
                                        Ext.each(item.items[0].data.receivers, function (receiver) {
                                            receiverShow = receiverShow + receiver.receiverName + '  ';
                                            receiverArray.push(Ext.data.StoreManager.lookup('receiversStore').findRecord('id', receiver.receiverId));
                                        });
                                        chooseReceiverGrid.setSelection(receiverArray);
                                    }//给收件人那一栏的文本框填空
                                    taskForm.down('#chooseReceiver').setValue(receiverShow);
                                    Ext.getCmp('taskDescription').setValue(item.items[0].data.description);
                                    //给任务类型赋值
                                    for (i = 0; i < typeStore.data.items.length; i++) {
                                        if (item.items[0].data.typeName == typeStore.data.items[i].data.typeName) {
                                            Ext.getCmp('taskType').setValue(typeStore.data.items[i].data.typeNum);
                                            break;
                                        }
                                    }
                                    //给任务优先级赋值
                                    for (i = 0; i < priorityStore.data.items.length; i++) {
                                        if (item.items[0].data.priorityName == priorityStore.data.items[i].data.priorityName) {
                                            Ext.getCmp('taskPriority').setValue(priorityStore.data.items[i].data);//不知道为什么只有这个combox要这样设置值
                                            break;
                                        }
                                    }
                                    //链接列表和附件列表要重新赋值
                                    attachedStore.setData(obj.attached);
                                    //判断任务和链接列表要不要收起来
                                    if (item.items[0].data.isAttachment == 1)
                                        taskForm.down('#taskFormAttachFieldset').setCollapsed(false);
                                    Ext.getCmp('centerRegion').add(taskForm);//将编辑任务的表单显示到中心区域
                                },
                                failure: function (response, opts) {
                                    Ext.Msg.alert("错误", "该功能暂时不可用！");
                                }
                            });
                        }
                    }
                }
            }, '-', {
                itemId: 'responserRemoveButton',
                text: '取消任务',
                tooltip: '取消任务',
                icon: 'static/images/icons/fam/delete.gif',
                disabled: true,
                listeners: {
                    click: function (button, e, eOpts) {
                        var item = taskResponseList.getSelectionModel().getSelected();//得到被选择的行的数组.
                        var flag = 1;
                        //循环判断选中项是否有不可操作项
                        for (i = 0; i < item.length; i++) {
                            if (item.items[i].data.statusName == '已取消') {
                                Ext.Msg.alert('警告', '选择项中包含已取消项！');
                                flag = 0;
                                return;
                            }
                        }
                        if (flag != 0) {
                            Ext.Msg.show({
                                title: '提示',
                                message: '你确认要取消这些任务了吗?',
                                buttons: Ext.Msg.YESNO,
                                icon: Ext.Msg.QUESTION,
                                fn: function (btn) {
                                    if (btn === 'yes') {
                                        var array = new Array();
                                        for (i = 0; i < item.length; i++) {
                                            array.push(item.items[i].data.id);
                                        }
                                        Ext.Ajax.request({
                                            url: zuulUrl + '/' + taskServiceName + '/status/responsible',
                                            method: 'put',
                                            defaultXdrContentType: 'application/json',
                                            jsonData: JSON.stringify(array),
                                            success: function (response, opts) {
                                                Ext.Msg.alert('成功', '任务取消成功');
                                                var count = 0;
                                                for (i = 0; i < item.length; i++) {
                                                    item.items[i].set('statusName', '已取消');//先手动修改本地store中的数据
                                                }
                                            },
                                            failure: function () {
                                                Ext.Msg.alert('错误', '有一项或多项操作失败！');
                                            }
                                        });
                                    }
                                }
                            });
                        }
                    }
                }
            }]
        }
    ],
    header: false,//取消标题头的显示
    iconCls: 'icon-grid',
    margin: '0 0 20 0',
    sortableColumns: false,//取消默认排序
    border: false
});

//添加展开监听事件
taskResponseList.getView().on('expandBody', function (rowNode, record, expandRow, eOpts) {
    getTaskDetialForResponsible(record);
});
